//-----------------------------------------------------------------------
// <copyright file="KeyWordSellDAOBase.cs" company="FetCo">
//     Copyright (c) FetCo. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

using System;
using System.Data;
using System.Data.Common;
using System.Collections;
using FetCo.Framework;
using FetCo.Framework.Data;
using Microsoft.Practices.EnterpriseLibrary.Data;
using ADESSA.Entities;

namespace ADESSA.Data
{    
    public abstract class KeyWordSellDAOBase
    {
        #region Common methods
        public virtual KeyWordSell CreateKeyWordSellFromReader(IDataReader reader)
        {
            KeyWordSell item = new KeyWordSell();
            try
            {
                if (!reader.IsDBNull(reader.GetOrdinal("KeyWordId"))) item.KeyWordId = (long)reader["KeyWordId"];
                if (!reader.IsDBNull(reader.GetOrdinal("KeyWord"))) item.KeyWord = (string)reader["KeyWord"];
                if (!reader.IsDBNull(reader.GetOrdinal("ProductBuyId"))) item.ProductBuyId = (long)reader["ProductBuyId"];
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessCreateKeyWordSellFromReaderException, ex);
            }
            return item;
        }
        #endregion
        
        #region CreateKeyWordSell methods
            
        public virtual void CreateKeyWordSell(KeyWordSell keyWordSell)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spKeyWordSellInsert");
                
                database.AddInParameter(dbCommand, "@KeyWord", DbType.String, keyWordSell.KeyWord);
                database.AddInParameter(dbCommand, "@ProductBuyId", DbType.Int64, keyWordSell.ProductBuyId);
                database.AddOutParameter(dbCommand, "@KeyWordId", DbType.Int64, 8);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
                keyWordSell.KeyWordId = (long)database.GetParameterValue(dbCommand, "@KeyWordId");
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessCreateKeyWordSellException, ex);
            }
        }

        #endregion

        #region UpdateKeyWordSell methods
        
        public virtual void UpdateKeyWordSell(KeyWordSell keyWordSell)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spKeyWordSellUpdate");            
                
                database.AddInParameter(dbCommand, "@KeyWordId", DbType.Int64, keyWordSell.KeyWordId);
                database.AddInParameter(dbCommand, "@KeyWord", DbType.String, keyWordSell.KeyWord);
                database.AddInParameter(dbCommand, "@ProductBuyId", DbType.Int64, keyWordSell.ProductBuyId);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessUpdateKeyWordSellException, ex);
            }
        }
        
        #endregion

        #region DeleteKeyWordSell methods
        public virtual void DeleteKeyWordSell(long keyWordId)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spKeyWordSellDelete");
                
                database.AddInParameter(dbCommand, "@KeyWordId", DbType.Int64, keyWordId);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessDeleteKeyWordSellException, ex);
            }
        }

        #endregion

        #region GetKeyWordSell methods
        
        public virtual KeyWordSell GetKeyWordSell(long keyWordId)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spKeyWordSellGet");
                
                database.AddInParameter(dbCommand, "@KeyWordId", DbType.Int64, keyWordId);
                
                KeyWordSell keyWordSell =  null;
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    if (reader.Read())
                    {
                        keyWordSell = CreateKeyWordSellFromReader(reader);
                        reader.Close();
                    }
                }
                return keyWordSell;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGetKeyWordSellException, ex);
            }
        }
        
        #endregion

        #region GetKeyWordSellList methods
        public virtual KeyWordSellCollection GetKeyWordSellList(KeyWordSellColumns orderBy, OrderDirection orderDirection, int page, int pageSize, out int totalRecords)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spKeyWordSellGetList");
                
                database.AddInParameter(dbCommand, "@OrderBy", DbType.AnsiString, orderBy.ToString());
                database.AddInParameter(dbCommand, "@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                database.AddInParameter(dbCommand, "@Page", DbType.Int32, page);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
                database.AddOutParameter(dbCommand, "@TotalRecords", DbType.Int32, 4);
                
                KeyWordSellCollection keyWordSellCollection = new KeyWordSellCollection();
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    while (reader.Read())
                    {
                        KeyWordSell keyWordSell = CreateKeyWordSellFromReader(reader);
                        keyWordSellCollection.Add(keyWordSell);
                    }
                    reader.Close();
                }
                totalRecords = (int)database.GetParameterValue(dbCommand, "@TotalRecords");
                return keyWordSellCollection;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGetKeyWordSellListException, ex);
            }
        }
        
        public virtual KeyWordSellCollection GetKeyWordSellList(KeyWordSellColumns orderBy, OrderDirection orderDirection)
        {            
            int totalRecords = 0;
            return GetKeyWordSellList(orderBy, orderDirection, 0, 0, out totalRecords);
        }
        
        #endregion
		
		  #region GetKeyWordSellList methods
        public virtual KeyWordSellCollection GetKeyWordSellListByParam(object orderBy, OrderDirection orderDirection, int page, int pageSize,string Criteria, out int totalRecords)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spKeyWordSellGetListByParam");
                
                database.AddInParameter(dbCommand, "@OrderBy", DbType.AnsiString, orderBy.ToString());
                database.AddInParameter(dbCommand, "@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                database.AddInParameter(dbCommand, "@Page", DbType.Int32, page);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
				database.AddInParameter(dbCommand, "@Criteria",  DbType.String, Criteria);
                database.AddOutParameter(dbCommand, "@TotalRecords", DbType.Int32, 4);
                
                KeyWordSellCollection keyWordSellCollection = new KeyWordSellCollection();
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    while (reader.Read())
                    {
                        KeyWordSell keyWordSell = CreateKeyWordSellFromReader(reader);
                        keyWordSellCollection.Add(keyWordSell);
                    }
                    reader.Close();
                }
                totalRecords = (int)database.GetParameterValue(dbCommand, "@TotalRecords");
                return keyWordSellCollection;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGetKeyWordSellListException, ex);
            }
        }
        
        public virtual KeyWordSellCollection GetKeyWordSellListByParam(object orderBy, OrderDirection orderDirection,string Criteria)
        {            
            int totalRecords = 0;
            return GetKeyWordSellListByParam(orderBy, orderDirection, 0, 0,Criteria, out totalRecords);
        }
        
        #endregion
    }
}